package com.example.serversocket;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
        new Thread(conn).start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

    @Override
    protected void onPause() {      
        super.onPause();
        if (server != null) {
//            try {
//            	Log.i("server", "closing server");
//                server.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }
    } 
    
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	
        if (server != null) {
          try {
          	Log.i("server", "closing server");
              server.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }    	
    }
	
	   /** Called when the activity is first created. */
    private static String TAG = "ServerSocketTest";

    private ServerSocket server;

    Runnable conn = new Runnable() {
    	
    	public void run() {
    		
    		try {
				ServerSocket Server = new ServerSocket 
						(8080, 10, InetAddress.getByName("127.0.0.1"));
				
				while(true) {
					Socket connected = Server.accept();
					    (new myHTTPServer(connected)).start();
					}
				
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		System.out.println ("TCPServer Waiting for client on port 5000");
    		
    	}
    	
        public void run2() {
            try {
                server = new ServerSocket(8080);

                while (true) {
                	Log.i("server", "entering loop");
                	
                    Socket socket = server.accept();
                    
                    Log.i("server", "new request");
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(socket.getInputStream()));
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());

                    
                    String str = in.readLine();

                    Log.i("received response from server", str);

                    out.writeBytes("HTTP/1.1 200 OK\r\n");
                    out.writeBytes("Content-Type: text/html\r\n\r\n");
                    out.writeBytes("<html><head></head><body><h1>Hello</h1></body></html>");

                    out.flush();
                    
                    in.close();
                    out.close();
                    socket.close();
                }
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } catch (Exception e) {             
                Log.e(TAG, e.getMessage());
            }
        }
    };

	
}
